package com.dragon.sdgp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.sdgp.common.R;
import com.dragon.sdgp.entity.SysUser;
import com.dragon.sdgp.mapper.SysUserMapper;
import com.dragon.sdgp.service.SysUserService;
import com.dragon.sdgp.util.JWTUtil;
import com.dragon.sdgp.util.PasswordUtil;
import com.dragon.sdgp.util.StringUtil;
import com.dragon.sdgp.util.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @AUTHOR : Dragon_Yang
 * @File : UserServiceImpl
 * @DESCRIPTION :
 * @DATE : 2023/3/12 16:05
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private VerificationCodeUtil verificationCodeUtil;

    @Override
    public SysUser getUserByEMailOrPhone(String account) {
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        if(account.contains("@")){
            userQueryWrapper.eq("email", account);
        }else {
            userQueryWrapper.eq("phone", account);
        }
        return getOne(userQueryWrapper);
    }

    @Override
    public R sysUserRegister(String code, SysUser sysUser) {
        if(!StringUtil.isEmpty(sysUser.getEmail()) || !StringUtil.isEmpty(sysUser.getPhone())){
            if(!verificationCodeUtil.verification(sysUser.getEmail(), code)){
                return R.fail(400, "验证码错误");
            }

            if(getUserByEMailOrPhone(sysUser.getEmail()) != null){
                return R.fail(400, "该账号已注册！");
            }
            sysUser.setPassword(PasswordUtil.encode(PasswordUtil.decrypt(code, sysUser.getPassword())));
            boolean save = save(sysUser);
            if(save){
                return R.success(200, sysUser.getEmail() + " 注册成功！", null);
            }
            return R.fail(400, "UNKNOWN ERROR");
        }
        return R.fail(400, "邮箱不能为空");
    }

    @Override
    public R sysUserLogin(SysUser sysUser) {
        
        if(!StringUtil.isEmpty(sysUser.getEmail()) || !StringUtil.isEmpty(sysUser.getPhone()) && !StringUtil.isEmpty(sysUser.getPassword())){
            sysUser.setPassword(PasswordUtil.decrypt(sysUser.getEmail(), sysUser.getPassword()));
            SysUser userByEMailOrPhone = getUserByEMailOrPhone(sysUser.getEmail());
            if(userByEMailOrPhone == null){
                return R.fail(400, "该账号不存在！");
            }
            if(userByEMailOrPhone.getEmail().equals(sysUser.getEmail()) && PasswordUtil.verify(sysUser.getPassword(), userByEMailOrPhone.getPassword())){
                String token = JWTUtil.Token(sysUser.getEmail());
                HashMap<String, String> data = new HashMap<String, String>();
                data.put("authorization", token);
                return R.success(200, "登录成功", data);
            }
            return R.fail(400, "账号或密码错误！");
        }
        return R.fail(400, "账号或密码不能为空！");
    }
}
